Skip to content

Fix parser error pointing to wrong line at EOF without trailing newline#1041

Merged
pelletier merged 1 commit intov2from
claude/fix-go-toml-1032-GHrkz
Mar 24, 2026
Merged

Fix parser error pointing to wrong line at EOF without trailing newline#1041
pelletier merged 1 commit intov2from
claude/fix-go-toml-1032-GHrkz

Conversation

@pelletier
Copy link
Copy Markdown
Owner

@pelletier pelletier commented Mar 24, 2026

Summary

  • Fix error highlight pointing to line 1 instead of the correct line when parsing a key missing = at EOF without a trailing newline (e.g., a = 1\nb = 2\nc)
  • The root cause was NewParserError receiving an empty byte slice as the highlight, causing subsliceOffset to return 0. Now passes the consumed key bytes (startB) as the highlight instead.
  • Add regression test for this scenario

Fixes #1032

Error output example

Given this input (no trailing newline after c):

a = 1
b = 2
c

Before (error incorrectly points to line 1):

1| a = 1
 |  expected = after a key, but the document ends there
2| b = 2
3| c

After (error correctly points to line 3):

1| a = 1
2| b = 2
3| c
 | ~ expected = after a key, but the document ends there

Test plan

  • All existing tests pass (go test ./...)
  • New test case TestDecodeError_Position/missing_equals_on_last_line_without_trailing_newline verifies error points to line 3
  • Manual verification confirms correct error output as shown above

https://claude.ai/code/session_01UWv8pyc8P1ktAPfHpveixj

…g newline

When parsing a key without '=' at EOF (e.g., "a = 1\nb = 2\nc"), the
error highlight was an empty slice, causing subsliceOffset to return 0
and the error to point at line 1 instead of line 3. Pass the consumed
key bytes as the highlight instead of the empty remainder.

Fixes #1032

https://claude.ai/code/session_01UWv8pyc8P1ktAPfHpveixj
@pelletier pelletier added the bug Issues describing a bug in go-toml. label Mar 24, 2026
@pelletier pelletier merged commit 16b1ef5 into v2 Mar 24, 2026
12 checks passed
@pelletier pelletier deleted the claude/fix-go-toml-1032-GHrkz branch March 24, 2026 01:34
Maks1mS pushed a commit to stplr-dev/stplr that referenced this pull request Mar 25, 2026
…374)

This PR contains the following updates:

| Package | Type | Update | Change | OpenSSF |
|---|---|---|---|---|
| [github.com/pelletier/go-toml/v2](https://github.com/pelletier/go-toml) | require | minor | `v2.2.4` → `v2.3.0` | [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/pelletier/go-toml/badge)](https://securityscorecards.dev/viewer/?uri=github.com/pelletier/go-toml) |

---

> ⚠️ **Warning**
>
> Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/23) for more information.

---

### Release Notes

<details>
<summary>pelletier/go-toml (github.com/pelletier/go-toml/v2)</summary>

### [`v2.3.0`](https://github.com/pelletier/go-toml/releases/tag/v2.3.0)

[Compare Source](pelletier/go-toml@v2.2.4...v2.3.0)

This is the first release built largely with the help of AI coding agents. Highlights include the complete removal of the unsafe package. go-toml is now fully safe Go code, with a geomean overhead of only \~1.4% vs v2.2.4 and zero additional allocations on benchmarks. This release also adds omitzero struct tag support, improves UnmarshalText/Unmarshaler handling for tables and array tables, and fixes several bugs including nil pointer marshaling, leap second handling, and datetime unmarshaling panics.

<!-- Release notes generated using configuration in .github/release.yml at v2.3.0 -->

#### What's Changed

##### What's new

- marshal: don't escape quotes unnecessarily by [@&#8203;virtuald](https://github.com/virtuald) in [#&#8203;991](pelletier/go-toml#991)
- Add `omitzero` tag support by [@&#8203;NathanBaulch](https://github.com/NathanBaulch) in [#&#8203;998](pelletier/go-toml#998)
- Support custom IsZero() methods with omitzero tag by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1020](pelletier/go-toml#1020)
- UnmarshalText fallbacks to struct unmarshaling for tables and arrays by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1026](pelletier/go-toml#1026)
- \[unstable] Support Unmarshaler interface for tables and array tables by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1027](pelletier/go-toml#1027)

##### Fixed bugs

- Add missing UnmarshalTOML call by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;996](pelletier/go-toml#996)
- Handle array table into an empty slice by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;997](pelletier/go-toml#997)
- Unwrap strict errors by [@&#8203;bersace](https://github.com/bersace) in [#&#8203;1012](pelletier/go-toml#1012)
- Fix leap second handling found by fuzz by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1019](pelletier/go-toml#1019)
- Fix nil pointer map values not being marshaled by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1025](pelletier/go-toml#1025)
- Fix panic when unmarshaling datetime values to incompatible types ([#&#8203;1028](pelletier/go-toml#1028)) by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1029](pelletier/go-toml#1029)
- Fix parser error pointing to wrong line at EOF without trailing newline by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1041](pelletier/go-toml#1041)

##### Documentation

- Improve Unmarshaling README by [@&#8203;heckelson](https://github.com/heckelson) in [#&#8203;1016](pelletier/go-toml#1016)
- Create AGENTS.md guidelines file by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1017](pelletier/go-toml#1017)

##### Other changes

- Unsafe package removal by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1021](pelletier/go-toml#1021)
- Bump CI and test scripts to Go 1.26 by [@&#8203;pelletier](https://github.com/pelletier) in [#&#8203;1030](pelletier/go-toml#1030)

#### New Contributors

- [@&#8203;virtuald](https://github.com/virtuald) made their first contribution in [#&#8203;991](pelletier/go-toml#991)
- [@&#8203;NathanBaulch](https://github.com/NathanBaulch) made their first contribution in [#&#8203;999](pelletier/go-toml#999)
- [@&#8203;bersace](https://github.com/bersace) made their first contribution in [#&#8203;1012](pelletier/go-toml#1012)
- [@&#8203;flyn-org](https://github.com/flyn-org) made their first contribution in [#&#8203;1013](pelletier/go-toml#1013)
- [@&#8203;heckelson](https://github.com/heckelson) made their first contribution in [#&#8203;1016](pelletier/go-toml#1016)

**Full Changelog**: <pelletier/go-toml@v2.2.4...v2.3.0>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At 12:00 AM through 04:59 AM and 10:00 PM through 11:59 PM, Monday through Friday ( * 0-4,22-23 * * 1-5 ), Only on Sunday and Saturday ( * * * * 0,6 ) (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44Ni4xIiwidXBkYXRlZEluVmVyIjoiNDMuODYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiS2luZC9EZXBlbmRlbmNpZXMiXX0=-->

Reviewed-on: https://altlinux.space/stapler/stplr/pulls/374
Co-authored-by: Renovate Bot <stapler-helper-bot@noreply.altlinux.space>
Co-committed-by: Renovate Bot <stapler-helper-bot@noreply.altlinux.space>
alliasgher added a commit to alliasgher/go-toml that referenced this pull request Apr 14, 2026
The subsliceOffset helper computed byte offsets by subtracting the
length of the slice from the total data length. This only works for
tail (suffix) slices; any sub-slice taken from the middle — such as
b[0:1] in parseSimpleKey's error path — returns an incorrect offset.

This was exposed by pelletier#1041, which correctly fixed a different error
position bug, but the existing formula is fundamentally wrong for
non-suffix slices.

Fix: use pointer arithmetic (via unsafe.Pointer) to compute the actual
offset of any subslice relative to p.data, regardless of its length.
An empty-slice fallback preserves the existing behaviour for the nil
case.

Fixes pelletier#1047

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues describing a bug in go-toml.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error on the last line produces parser error pointing to wrong line

2 participants